const hre = require('hardhat');

/**
 * 检查XLayerLaunchPad合约的常量配置
 */
async function main() {
  const launchPadAddr = process.env.LAUNCHPAD_ADDRESS;
  if (!launchPadAddr) {
    throw new Error('环境变量 LAUNCHPAD_ADDRESS 必须设置');
  }

  console.log('=== XLayer LaunchPad Constants ===');
  console.log('Network:', hre.network.name);
  console.log('LaunchPad Address:', launchPadAddr);

  const [signer] = await hre.ethers.getSigners();
  const launchPad = await hre.ethers.getContractAt('XLayerLaunchPad', launchPadAddr, signer);

  try {
    const minTarget = await launchPad.MIN_TARGET();
    const maxTarget = await launchPad.MAX_TARGET();
    const graduationThreshold = await launchPad.GRADUATION_THRESHOLD();
    const platformFee = await launchPad.platformFeePercent();

    console.log('\n=== Constants ===');
    console.log('MIN_TARGET:', hre.ethers.formatEther(minTarget), 'ETH');
    console.log('MAX_TARGET:', hre.ethers.formatEther(maxTarget), 'ETH');
    console.log('GRADUATION_THRESHOLD:', graduationThreshold.toString(), '%');
    console.log('Platform Fee:', platformFee.toString(), 'basis points (', (Number(platformFee) / 100).toFixed(2), '%)');

    // 检查具体的launch
    if (process.env.LAUNCH_ID) {
      const launchId = parseInt(process.env.LAUNCH_ID);
      console.log('\n=== Launch Info for ID', launchId, '===');
      
      const launchInfo = await launchPad.getLaunchInfo(launchId);
      console.log('Token Address:', launchInfo[0]);
      console.log('Creator:', launchInfo[1]);
      console.log('Name:', launchInfo[2]);
      console.log('Symbol:', launchInfo[3]);
      console.log('Target Amount:', hre.ethers.formatEther(launchInfo[5]), 'ETH');
      console.log('Raised Amount:', hre.ethers.formatEther(launchInfo[6]), 'ETH');
      console.log('Launched:', launchInfo[8]);
      console.log('Cancelled:', launchInfo[9]);
      
      const raisedAmount = Number(hre.ethers.formatEther(launchInfo[6]));
      const targetAmount = Number(hre.ethers.formatEther(launchInfo[5]));
      const threshold = Number(graduationThreshold);
      const requiredAmount = targetAmount * threshold / 100;
      
      console.log('\n=== Graduation Analysis ===');
      console.log('Raised:', raisedAmount, 'ETH');
      console.log('Target:', targetAmount, 'ETH');
      console.log('Required for graduation:', requiredAmount, 'ETH');
      console.log('Progress:', ((raisedAmount / targetAmount) * 100).toFixed(2), '%');
      console.log('Can graduate:', raisedAmount >= requiredAmount ? 'YES' : 'NO');
      
      if (raisedAmount >= requiredAmount && !launchInfo[8]) {
        console.log('\n⚠️  This token should have graduated but hasn\'t!');
      }
    }

  } catch (error) {
    console.error('Error reading contract:', error);
  }
}

main().catch(e => {
  console.error(e);
  process.exit(1);
});
